<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.perfectNumber"
        placeholder="输入用户ID"
        style="width: 200px; margin-right: 20px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="query_white">
        查询
      </el-button>
      <el-button
        class="filter-item"
        style="float: right"
        type="primary"
        icon="el-icon-edit"
        @click="dialogVisible = true"
      >
        添加白名单
      </el-button>
    </div>

    <el-table :data="list" stripe style="width: 860px" border>
      <el-table-column prop="perfect_number" label="ID" width="100" align="center" />
      <el-table-column prop="nickname" label="昵称" width="200" align="center" />
      <el-table-column prop="add_time" label="添加时间" align="center" width="300" />
      <el-table-column fixed="right" label="操作" width="250" align="center">
        <template slot-scope="scope" align="center">
          <el-button type="text" size="small" @click="editClick(scope.row)">
            移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加白名单" :visible.sync="dialogVisible" width="400px" center>
      <el-input v-model="input" placeholder="输入ID或手机号" />
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" style="width: 100%" @click="AddWhitelist">确 定</el-button>
      </span>
    </el-dialog>
    <pagination
      v-show="listQuery.total > 0"
      :total="listQuery.total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="handleFilter"
    />
  </div>
</template>

<script>
import {
  encryption2
} from '@/api/index'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ComplexTable',
  components: {
    Pagination
  },
  data() {
    return {
      dialogVisible: false,
      listQuery: {
        total: 0,
        pageNo: 1,
        pageSize: 10,
        perfectNumber: ''
      },
      list: [],
      input: ''
    }
  },
  created() {
    this.handleFilter()
  },
  methods: {
    // 确认添加
    AddWhitelist() {
      if (!this.input) {
        return this.$message({
          message: '搜索框不能为空',
          type: 'error'
        })
      }
      const arys = {
        keyword: this.input
      }
      const url = '/user/addWhite'
      encryption2(arys, url).then((res) => {
        if (res.data.status === 0) {
          this.$message({
            message: res.data.text,
            type: 'success'
          })
          this.handleFilter()
        } else {
          this.$message({
            message: res.data.text,
            type: 'error'
          })
        }
        this.input = ''
        this.dialogVisible = false
      })
    },
    //   移除提示
    editClick(row) {
      const _ = this
      this.$confirm(`确认移除用户: ${row.nickname} 吗`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          _.editwhite(row)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //   9166391
    // 确认移除白名单
    editwhite(row) {
      const arys = {
        id: row.id
      }
      const url = '/user/deleteWhite'
      encryption2(arys, url).then((res) => {
        if (res.data.status === 0) {
          this.$message({
            message: res.data.text,
            type: 'success'
          })
          this.handleFilter()
        } else {
          this.$message({
            message: res.data.text,
            type: 'error'
          })
        }
      })
    },
    query_white() {
      (this.listQuery.pageNo = 1), this.handleFilter()
    },
    // 查询
    handleFilter() {
      const arys = {
        perfectNumber: this.listQuery.perfectNumber,
        pageNo: this.listQuery.pageNo,
        pageSize: this.listQuery.pageSize
      }
      const url = '/user/whiteList'
      encryption2(arys, url).then((res) => {
        if (res.data.status === 0) {
          this.list = res.data.result.list
          this.listQuery.total = res.data.result.count
          // this.$message({
          //   message: res.data.text,
          //   type: "success",
          // });
        } else {
          this.$message({
            message: res.data.text,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
<style scoped>
  .filter-container .filter-item {
    margin-bottom: 0;
  }

  .el-table--border {
    border-right: 1px solid #dfe6ec;
  }

  .filter-container {
    width: 860px;
  }
</style>
